package collect.taobao.util.page;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class pageSql {
    public pageSql() {

    }
    
    public PageBuffer queryPage(String sqlStr, Object beanName, Connection conn,
            int CurrentPage, int PageSize, String listURL) {
        int count = 0;
        int length = 0;
        String countSql = "";

        if (sqlStr.toLowerCase().indexOf("union") != -1) {
            countSql = "select count(*)  from ( " + sqlStr + " )  ";

        } else {
            length = sqlStr.length();
            int j = 0;
            String temsql = sqlStr.substring(sqlStr.toLowerCase().indexOf(
                    "select") + 6, length);
            int tempLen = temsql.length();
            for (int i = 0; i < tempLen; i++) {
                if (temsql.toLowerCase().indexOf("select") != -1) {

                    if (temsql.toLowerCase().indexOf("select") < temsql
                            .toLowerCase().indexOf("from")) {

                        temsql = temsql.substring(temsql.toLowerCase().indexOf(
                                "from") + 4, temsql.length());
                        tempLen = temsql.length();
                    } else {
                        countSql = temsql.substring(temsql.toLowerCase()
                                .indexOf("from"), temsql.length());
                        break;
                    }
                } else {
                    countSql = temsql.substring(temsql.toLowerCase()
                            .lastIndexOf("from"), temsql.length());
                    break;
                }
            }

            int cc=countSql.toLowerCase().lastIndexOf(" order ");
            if(cc!=-1 )
            {
            	countSql=countSql.substring(0,cc);
            }
            countSql = "select count(*) " + countSql;
        }

       

        
        PageBuffer page = PageBuffer.EMPTY_PAGE;
        try {
            Statement stmt = conn.createStatement();
            PageBuild pBuilder = (PageBuild) beanName;

            ResultSet rs1 = stmt.executeQuery(countSql);
            if (rs1.next())
                count = rs1.getInt(1);
            rs1.close();

            sqlStr = getLimitString(sqlStr, true, PageSize, CurrentPage);

            ResultSet rs = stmt.executeQuery(sqlStr);

            page = pBuilder
                    .buildPage(rs, CurrentPage, PageSize, count, listURL);

            rs.close();
            stmt.close();
        } catch (SQLException ex) {
        	ex.printStackTrace();
        	System.out.println("cuole");
        }

        return page;
    }


    
    public static String getLimitString(String sql, boolean hasOffset,
            int length, int pageNo) {
        StringBuffer pagingSelect = new StringBuffer(sql.length() + 100);

        int start = (pageNo - 1) * length;
        int end = start + length;

        if (hasOffset) {
            pagingSelect
                    .append(" ");
        } else {
            pagingSelect.append(" ");
        }

        pagingSelect.append(sql);
            pagingSelect.append(" limit "+start+" , "+2);
        System.out.println("pagingSelect"+pagingSelect.toString());
        return pagingSelect.toString();
    }
    
    
}
